package es.cazadoresmadridejos.beans.dao.deer.impl;

import java.sql.SQLException;
import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import es.cazadoresmadridejos.beans.dao.deer.DeerDao;
import es.cazadoresmadridejos.common.bean.dao.impl.DaoImpl;
import es.cazadoresmadridejos.model.Deer;
import es.cazadoresmadridejos.objs.web.Page;
import es.cazadoresmadridejos.objs.web.PaginatedListImpl;

@SuppressWarnings("unchecked")
@Repository("deerDao")
public class DeerDaoImpl extends DaoImpl<Deer> implements DeerDao{

	@Override
	public Deer newInstance() {
		return new Deer();
	}

	@Override
	public List<Deer> findAllAssitances(final Long id) {
		return (List<Deer>) getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
					.append("select d ")
					.append(" from Deer d ")
					.append(" where d.reason.id = :reasonid ")
					.append("");

				Query query = session.createQuery(hql.toString());
				
				query.setLong("reasonid", id);
				
				return query.list();
			}
		});
	}

	@Override
	public void deleteNotIn(final List<Long> list, final Long reasonid) {
		getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {

				StringBuilder hql = new StringBuilder();
				hql
				.append("delete from ")
				.append(" Deer d ")
				.append(" where d.id not in (:deerList) ")
				.append(" 	and d.reason.id = :reasonid ")
				.append("");
				
				Query query = session.createQuery(hql.toString());
				
				query.setParameterList("deerList", list);
				query.setLong("reasonid", reasonid);

				return query.executeUpdate();
			}
		});
	}

	@Override
	public PaginatedList findAllPaginated(final Page page, final Long id) {
		return (PaginatedList) getHibernateTemplate().execute(new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				if("id".equals(page.getSort())){
					page.setSort("date");
				}
				if(page.getSortOrder() == null){
					page.setSortOrder(SortOrderEnum.DESCENDING);
				}

				StringBuilder hql = new StringBuilder();
				StringBuilder hqlCount = new StringBuilder();
				hql
					.append("select d ")
					.append(" from Deer d")
					.append(" where d.partner.id = :partnerId ")
					.append(getSort(page, "d"));
				
				hqlCount
					.append("select count(d) ")
					.append(" from Deer d")
					.append(" where d.partner.id = :partnerId ")
					.append("");
				
				
				Query query = session.createQuery(hql.toString());
				query.setLong("partnerId", id);
				
				Query queryCount = session.createQuery(hqlCount.toString());
				queryCount.setLong("partnerId", id);

				return new PaginatedListImpl<Object>(query.list(), 
						page, 
						((Long)queryCount.uniqueResult()).intValue());
			}
		});
	}

}
